package com.yzyz.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 
 * @author david.sun
 * @date 2010-7-14
 */
public class ValidateKey {
	private int width 	= 130;
	private int height 	= 40;
	private BufferedImage image;
	private Graphics2D g2D;
	
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public ValidateKey(int width, int height, String backgroundColor){
		this.width = width;
		this.height = height;
		
		image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
		g2D = image.createGraphics();
		
		g2D.setColor(Color.decode(backgroundColor));
		g2D.fillRect(0, 0, this.width, this.height); 
	}
	public void drawSlogan(String slogan,int xFrom,int yFrom, int fontSize) {
		g2D.setFont(new Font("Times New Roman",Font.BOLD,fontSize));
		g2D.setColor(Color.decode("#00CD00"));
		g2D.drawString(slogan, xFrom, yFrom);
	}
	public ValidateKey(){
		image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
		g2D = image.createGraphics();
		
		g2D.setColor(Color.decode("#C0FF3E"));
		g2D.fillRect(0, 0, this.width, this.height); 
	}
	
	public ValidateKey(String backgroundColor){
		image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
		g2D = image.createGraphics();
		
		g2D.setColor(Color.decode(backgroundColor));
		g2D.fillRect(0, 0, this.width, this.height); 
	}
	public OutputStream getImage(OutputStream out) throws ImageFormatException, IOException{
		g2D.dispose();
		JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		return out;
	}
	public void drawLine(String lineColor, int xFrom, int yFrom, int xEnd, int yEnd) {
		this.g2D.setColor(Color.decode(lineColor));
		this.g2D.drawLine(xFrom, yFrom, xEnd, yEnd);
	}
	public OutputStream getDefaultImage(char [] chars, OutputStream out) throws ImageFormatException, IOException{
		if(image == null) {
			image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
			g2D = image.createGraphics(); 
		}

		g2D.setColor(Color.decode("#C0FF3E"));
		g2D.fillRect(0, 0, this.width, this.height);  

		g2D.setFont(new Font("Times New Roman",Font.BOLD,30));

		int x = 10;
		for(char c: chars) {
			g2D.setColor(Color.decode("#00CD00"));
			g2D.drawString(String.valueOf(c), x, 30);
			x = x + 20;
			
		}
		
		g2D.dispose();
		JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		return out;
	}
	
	public void closeOut(OutputStream out) {
		if(out == null) {
			return;
		}
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String [] args){
		
		File f = new File("e:\\default.jpg");
		if(f.exists()) {
			f.delete();
		}
		if(!f.exists()){
			try {
				f.createNewFile();
				FileOutputStream fo = new FileOutputStream(f);
				ValidateKey d = new ValidateKey("#C0FF3E");
				
				d.setHeight(40);
				d.setWidth(150);
				d.drawSlogan("A B C D", 4, 30, 30);
				d.drawLine("#FF34B3", 0, 15, 130, 15);
				d.drawLine("#1C86EE", 0, 20, 130, 20);
				d.drawLine("#8B008B", 0, 35, 130, 35);
				d.getImage(fo);
				d.closeOut(fo);
				
			} catch (IOException e) {
				
			}
		}
	}
	
}
